using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace VisualStudioMruFileUtil
{
	internal abstract class PersistentWindowState
	{
		// Fields
		private Form form;
		private string topKey = "Top";
		private string heightKey = "Height";
		private string widthKey = "Width";
		private string baseKeyName = "";
		private string H6R78NRBA1 = "State";
		private FormWindowState formState = FormWindowState.Normal;
		protected int height = 400;
		private string leftKey = "Left";
		protected int left = 50;
		protected int top = 50;
		protected int width = 600;

		// Methods
		public PersistentWindowState(Form form, string subKeyBaseName)
		{
			if ( (subKeyBaseName == null) || (subKeyBaseName.Length == 0) )
			{
				ArgumentException exception = new ArgumentException( "Invalid SubKeyBaseName", "SubKeyBaseName" );
				exception.Source = "PersistantWindowState class";
				throw exception;
			}
			if ( form == null )
			{
				ArgumentException exception2 = new ArgumentException( "Invalid Form form", "form" );
				exception2.Source = "PersistantWindowState class";
				throw exception2;
			}
			this.form = form;
			char[] trimChars = new char[] { '\\' };
			subKeyBaseName = subKeyBaseName.TrimStart( trimChars );
			subKeyBaseName = subKeyBaseName.TrimEnd( trimChars );
			this.baseKeyName = @"Software\" + subKeyBaseName + @"\PersistantWindowState";
			this.width = form.Width;
			this.height = form.Height;
			this.left = form.Left;
			this.top = form.Top;
		}

		private string FormStateToText(FormWindowState state)
		{
			switch ( state )
			{
				case FormWindowState.Normal:
					return "Normal";

				case FormWindowState.Minimized:
					return "Minimized";

				case FormWindowState.Maximized:
					return "Maximized";
			}
			return "Normal";
		}

		private FormWindowState TextToFormState(string stateText)
		{
			FormWindowState normal = FormWindowState.Normal;
			string str = stateText;
			if ( str == null )
			{
				return normal;
			}
			str = string.IsInterned( str );
			if ( str == "Normal" )
			{
				return FormWindowState.Normal;
			}
			if ( str == "Maximized" )
			{
				return FormWindowState.Maximized;
			}
			if ( str != "Minimized" )
			{
				return normal;
			}
			return FormWindowState.Minimized;
		}

		public void InitializeWindow()
		{
			this.form.Width = this.Width;
			this.form.Height = this.Height;
			this.form.Left = this.Left;
			this.form.Top = this.Top;
			this.form.WindowState = this.WindowState;
		}

		public void SaveWindow()
		{
			if ( this.form.WindowState != FormWindowState.Minimized )
			{
				if ( this.form.WindowState == FormWindowState.Normal )
				{
					this.Width = this.form.Width;
					this.Height = this.form.Height;
					this.Top = this.form.Top;
					this.Left = this.form.Left;
				}
				this.WindowState = this.form.WindowState;
			}
		}

		// Properties
		public int Height
		{
			get
			{
				try
				{
					RegistryKey key = Registry.CurrentUser.OpenSubKey( this.baseKeyName );
					this.height = (int)key.GetValue( this.heightKey, this.height );
				}
				catch
				{
				}
				return this.height;
			}
			set
			{
				Registry.CurrentUser.CreateSubKey( this.baseKeyName ).SetValue( this.heightKey, value );
			}
		}

		public int Left
		{
			get
			{
				try
				{
					RegistryKey key = Registry.CurrentUser.OpenSubKey( this.baseKeyName );
					this.left = (int)key.GetValue( this.leftKey );
				}
				catch
				{
				}
				return this.left;
			}
			set
			{
				Registry.CurrentUser.CreateSubKey( this.baseKeyName ).SetValue( this.leftKey, value );
			}
		}

		public int Top
		{
			get
			{
				try
				{
					RegistryKey key = Registry.CurrentUser.OpenSubKey( this.baseKeyName );
					this.top = (int)key.GetValue( this.topKey );
				}
				catch
				{
				}
				return this.top;
			}
			set
			{
				Registry.CurrentUser.CreateSubKey( this.baseKeyName ).SetValue( this.topKey, value );
			}
		}

		public int Width
		{
			get
			{
				try
				{
					RegistryKey key = Registry.CurrentUser.OpenSubKey( this.baseKeyName );
					this.width = (int)key.GetValue( this.widthKey, this.width );
				}
				catch
				{
				}
				return this.width;
			}
			set
			{
				Registry.CurrentUser.CreateSubKey( this.baseKeyName ).SetValue( this.widthKey, value );
			}
		}

		public FormWindowState WindowState
		{
			get
			{
				string str = "";
				try
				{
					str = (string)Registry.CurrentUser.OpenSubKey( this.baseKeyName ).GetValue( this.H6R78NRBA1, "Normal" );
					this.formState = TextToFormState( str );
				}
				catch
				{
				}
				return this.formState;
			}
			set
			{
				Registry.CurrentUser.CreateSubKey( this.baseKeyName ).SetValue( this.H6R78NRBA1, value );
			}
		}
	}
}
